;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.MaterialStorage", [])
        .controller("MaterialStorageCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "EventBus", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, EventBus) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.datePickerFlag = [false, false];
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1800,
                tableCol: [
                    {
                        type: "deliveryDate",
                        name: "交付日期",
                        dataField: "deliveryDate",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "lackRemark",
                        name: "交付说明",
                        dataField: "lackRemark",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户编号",
                        dataField: "dealerCode",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "dealerName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单据编号",
                        dataField: "billNo",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        clickFn: "viewDetail",
                    },
                    {
                        type: "",
                        name: "单据类型",
                        dataField: "billType",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购级别",
                        dataField: "purchaseLevelName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购员",
                        dataField: "purchaseUser",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "调度员",
                        dataField: "planUser",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "制单人",
                        dataField: "createBillUserName",
                        width: 100,
                        isShow: $scope.isDL || $scope.isZHL || $scope.isYH,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "remark",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库类型",
                        dataField: "outType",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createTimeShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件分类",
                        dataField: "materialsType",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "采购组",
                        dataField: "purchaseGroup",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单价",
                        dataField: "price",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "需求数量",
                        dataField: "needQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "未分发数量",
                        dataField: "notInformQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "总库存",
                        dataField: "allInventory",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划外仓库库存",
                        dataField: "outInventory",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划内仓库库存",
                        dataField: "inInventory",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "缺货数量",
                        dataField: "lackNum",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        clickFn: "toSaleOrderPage",
                    },
                    {
                        name: "标准交期",
                        dataField: "standDeliveryTime",
                        width: 130,
                        isShow: true,
                    },
                    {
                        name: "预计发货日期",
                        dataField: "estimatedShippingDateShow",
                        width: 130,
                        isShow: true,
                    }
                ]
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "toSaleOrderPage":
                        toSaleOrderPage(item);
                        break;
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            function viewDetail(item) {
                var url = "";
                switch (item.billType) {
                    case "销售单":
                        url = $state.href("app.sales.sellIntention");
                        window.sessionStorage.setItem("sellIntentionBillNo", item.billNo);
                        break;
                    case "其他出库单":
                        url = $state.href("app.stock.unusualOut");
                        window.sessionStorage.setItem("unusualOutBillNo", item.billNo);
                        break;
                    case "三包单":
                        url = $state.href("app.sales.guaranteesApply");
                        window.sessionStorage.setItem("guaranteesApplyBillNo", item.billNo);
                        break;
                    case "区域调拨单":
                        url = $state.href("app.stock.allocation");
                        window.sessionStorage.setItem("regionAllotOrder", item.billNo);
                        break;
                    default:
                        break;
                }
                window.open(url, '_blank');
            }

            function toSaleOrderPage(item) {
                var url = $state.href("app.report.saleOrder");
                var paramObj = {};
                paramObj.materialsNo = item.materialsNo;
                paramObj.billNo = item.billNo;
                window.sessionStorage.setItem("saleOrderPageParam", JSON.stringify(paramObj));
                window.open(url, '_blank');
            }

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/report/materialStorageExport"),
                sectionParam: {materialsNos: []},
                allUrl: ybHttp.RequestUrl("device", "/report/materialStorageExportAll")
            };

            var btnPermissionCode = {
                export: window.ActionCode.materialStorageExport,
                import: window.ActionCode.materialStorageImport,
            };

            $scope.btnPermission = {
                export: true,
                import: true,
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.switchListDatePicker = function (item) {
                angular.forEach($scope.dataList, function (item) {
                    item.showDatePicker = false;
                });
                item.showDatePicker = true;
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.materialsNos = [];
                var service = "device";
                var reqUrl = "/report/materialStorage";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createTimeShow = $filter("YbDate")(item.createTime, "yyyy-MM-dd HH:mm:ss");
                        item.estimatedShippingDateShow = $filter("YbDate")(item.estimatedShippingDate, "yyyy-MM-dd HH:mm:ss");
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.materialsNos = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.materialsNos.push(item.materialsNo);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };

            $scope.changeDeliveryDate = function (item) {
                var service = "device";
                var reqUrl = "/saleIntention/updateDeliveryDate";
                var param = {};
                param.saleNo = item.billNo;
                param.materialsNo = item.materialsNo;
                param.deliveryDate = item.deliveryDate;
                if (!param.deliveryDate) {
                    ybWidget.ShowToaster('error', '请选择交付时间');
                    return;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            $scope.changeLackRemark = function (item) {
                var service = "device";
                var reqUrl = "/saleIntention/updateLackRemark";
                var param = {};
                param.saleNo = item.billNo;
                param.materialsNo = item.materialsNo;
                param.lackRemark = item.lackRemark;
                if (!param.lackRemark) {
                    ybWidget.ShowToaster('error', '请填写备注');
                    return;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            $scope.importData = function () {
                var param = {
                    name: "缺货报表",
                    templateReqService: "device",
                    templateReqUrl: "/saleIntention/downloadLackTemplate",     // 模板路径
                    importReqService: "device",
                    importReqUrl: "/saleIntention/importLackInfo",      // 导入接口
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                if ($scope.isDL || $scope.isZHL || $scope.isYH){
                    $scope.condition.outOfStockMark = "1";
                    $scope.condition.createBillUserName = $scope.curUser.name;
                }
            })();
        }])
    ;
})(window, window.angular);